import fetch from 'dva/fetch';
import { createAuthorizationTokenHeader } from './auth';

export default async function request(url, options) {
  options = { headers: createAuthorizationTokenHeader(), ...options };
  console.log("request url=> ", url);
  console.log("request options=> ", options);
  const response = await fetch(url, options);
  if (response.status === 401 && response.url.indexOf("/api/auth/login") === -1) {
    localStorage.clear();
    window.location.href = "/login";
  } else if (response.status >= 200 && response.status < 300) {
    const ret = await response.json();
    console.log("success, response json=> ", ret);
    return ret;
  } else {
    return { status: false, errorCode: response.status };
  }
}
